<?php

namespace Maestros\Controller\Service;

use Zend\Db\Adapter\Adapter;
use Componentes\Controller\UtilsArrayController;
use Maestros\Model\Entity\Hora;
use Maestros\Controller\Service\GenericService;
use Maestros\Model\Dao\HoraDAO;
use Maestros\Controller\Service\TurnoService;

//SERVICIO
class HoraService extends GenericService {

    /**
     * @var Zend\Db\Adapter\Adapter 
     */
    private $dbAdapter;
    private static $horaService;

    /**
     * @param \Zend\Db\Adapter\Adapter $dbAdapter
     */
    private function __construct(Adapter $dbAdapter) {
        $this->dbAdapter = $dbAdapter;
    }

    public static function getInstance($adapter) {
        if (self::$horaService == null) {
            self::$horaService = new HoraService($adapter);
        }
        return self::$horaService;
    }

    public function getHoraDao() {
        return HoraDAO::getInstance($this->dbAdapter);
    }

    /**
     * @param type $id
     * @return type
     */
    public function getRegistro($id) {
        $data = null;
        $success = false;
        $mensaje = 'HORA NO ENCONTRADO';
        $hora = $this->obtenerHoraSegunId($id);
        if ($hora->getId() != null) {
            $data[0] = $hora->objectToArray();
            $mensaje = 'HORA ENCONTRADO';
            $success = true;
        }
        $output = array(
            'success' => $success,
            'data' => $data,
            'total' => count($data),
            'mensaje' => $mensaje
        );
        return $output;
        //return UtilsArrayController::organizarOutput($success, $data, null);
    }

//POR HACER
    public function getRegistroCompleto($id) {
        $hora = $this->obtenerHoraSegunId($id);
        return $this->armarRegistroCompleto($hora);
    }

    public function armarRegistroCompleto($hora) {
        $mensaje = 'HORA NO REGISTRADA';
        $data = null;
        $success = false;
        $servicioTurno = TurnoService::getInstance($this->dbAdapter);
        $turno = $servicioTurno->obtenerTurnoSegunId($hora->getTurno());
        if ($hora->getId() != null) {
            $data[0] = $hora->objectToArray();
            $success = true;
            $data[0]['_turno'] = $turno->getDescripcion();
            $mensaje = 'HORA ENCONTRADA';
        }
        $output = array(
            'success' => $success,
            'data' => $data,
            'total' => count($data),
            'mensaje' => $mensaje
        );
        return $output;
    }

    public function obtenerHoraSegunId($id) {
        $registros = $this->getHoraDao()->getRegistro($id);
        $data = UtilsArrayController::llenarData($registros);
        $hora = new Hora($data[0]);
        $hora->setTurno($data[0]['id_turno']);
        return $hora;
    }

}
